//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 21
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CallGetUserAvailAndCheckResponse
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // Need to build up a valid GetUserAvailabilityRequestType before calling into the 
            // CallGetUserAvailAndCheckResponse method.
            //
        }

        /// <summary>
        /// Calls the GetUserAvailabilityRequest method and parses the response for
        /// any errors. (Listing 21-12)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="request">request with all relivant properties defined</param>
        /// <param name="didErrorsOccur">true if any errors were present in the response
        /// </param>
        /// <param name="allErrors">StringBuilder of all errors that were 
        /// present</param>
        /// <returns>The server response</returns>
        public static GetUserAvailabilityResponseType CallGetUserAvailAndCheckResponse(
            ExchangeServiceBinding binding,
            GetUserAvailabilityRequestType request,
            out bool didErrorsOccur,
            out StringBuilder allErrors)
        {
            // Call the GetUserAvailabilityRequest web method
            //
            GetUserAvailabilityResponseType response =
                binding.GetUserAvailability(request);

            // A GetUserAvailiability response can contain lots of error response codes,
            // but still contain usefull information, therefore this method will not 
            // throw an exception when it detects an error, but rather store that error 
            // in a string buffer and return it to the caller.
            //
            allErrors = new StringBuilder();

            // Our response will have one FreeBusyResponseType for each participant, and
            // only if the request included FreeBusyViewOptions. Therefore, when 
            // checking for success, first check that the array is populated, and then 
            // check that each element has a success response message attached to it.
            //
            FreeBusyResponseType[] fbResponseArray = response.FreeBusyResponseArray;
            if (fbResponseArray != null)
            {
                for (int x = 0; x < fbResponseArray.Length; x++)
                {
                    if (fbResponseArray[x].ResponseMessage.ResponseClass !=
                        ResponseClassType.Success)
                    {
                        allErrors.AppendLine("There was a problem with particpant #" + x
                            + "\r\n" + fbResponseArray[x].ResponseMessage.ResponseCode +
                            "\r\n" + fbResponseArray[x].ResponseMessage.MessageText);
                    }
                    else
                    {
                        // Hint: You could put a call to the PrintFreeBusyInformation
                        // method from Listing 21-37 right here
                    }
                }
            }

            // Unlike the FreeBusyResponseArray which includes one per participant, 
            // there is only going to be one SuggesstionsResponse with a response 
            // message.
            //
            SuggestionsResponseType sgResponse = response.SuggestionsResponse;
            if (sgResponse != null)
            {
                if (sgResponse.ResponseMessage.ResponseClass !=
                    ResponseClassType.Success)
                {
                    allErrors.AppendLine("There was a problem with the suggestions " +
                        "response.\r\n" + sgResponse.ResponseMessage.ResponseCode +
                        "\r\n" + sgResponse.ResponseMessage.MessageText);
                }
                else
                {
                    // Hint: You could put a call to the PrintSuggestionsInformation
                    // method from Listing 21-54 right here
                }
            }

            didErrorsOccur = (allErrors.Length > 0);

            return response;
        }

    }
}
